﻿using System;
using System.Web;
using System.Web.Mvc;
using TidyBlog.MVC;
using TidyBlog.Entity;
using TidyBlog.BLL;

namespace TidyBlog.MVC.WWW.Controllers
{
    /// <summary>
    /// 链接管理控制器 /Manage_Link/{action}
    /// </summary>
    public class Manage_LinkController : ManageController
    {
        /// <summary>
        /// 链接列表
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            return View(LinkBLL.List(0));
        }

        /// <summary>
        /// 删除链接
        /// </summary>
        /// <param name="id">链接编号</param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            LinkBLL.Delete(id);

            RespondentInfo.Success(this.ControllerContext, new OperInfo()
            {
                Message = "已删除指定友情链接",
                Link = Url.Action("List")
            });

            return new EmptyResult();
        }

        /// <summary>
        /// 创建链接界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// 创建链接
        /// </summary>
        /// <param name="link">链接</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(Link link)
        {
            LinkBLL.Create(link);

            RespondentInfo.Success(this.ControllerContext, new OperInfo()
            {
                Message = "已添加友情链接" + link.LinkName,
                Link = Url.Action("List"),
                LinkText = "返回列表"
            });

            return new EmptyResult();
        }

        /// <summary>
        /// 编辑链接界面
        /// </summary>
        /// <param name="id">链接编号</param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var link = LinkBLL.Read(id);

            if (link == null)
            {
                throw new Exception("友情链接不存在");
            }

            return View(link);
        }

        /// <summary>
        /// 更新链接
        /// </summary>
        /// <param name="link">链接</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Link link)
        {
            LinkBLL.Update(link);

            RespondentInfo.Success(this.ControllerContext, new OperInfo()
            {
                Message = "已更新友情链接 " + link.LinkName,
                Link = Url.Action("List"),
                LinkText = "返回列表"
            });

            return new EmptyResult();
        }
    }
}
